<?php

/**
 *
 * @author Administrator
 */
class ServiceController {
    
    // TODO - Insert your code here
    private $path;
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/view_service/temp/";
        $this->hotArray = array(1 => "Dịch vụ nổi bật", 0 => "Không phải Dịch vụ bật");
        $this->feshArray = array(1 => "Dịch vụ mới", 0 => "Không phải Dịch vụ mới");

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * view table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewTable() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_SERVICES);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        $template->assign( CID, $_REQUEST[PARAM_CID]);
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['serviceShortColArray'], SERVICES, array(SERVICE_CATEGORYID => $_REQUEST[PARAM_CID], SQL_AND, SERVICE_LANGUAGE => $this->lan), array( SERVICE_UPDATETIME => DESC));
        
        // assign item
        foreach ( $list as $item ) {
            $cate = $db->selectOneByCause(array(CATEGORY_TITLE), CATEGORIES, array(CATEGORY_ID => $item[SERVICE_CATEGORYID]));
            $item[SERVICE_CATEGORYID] = $cate[CATEGORY_TITLE];
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        $cate = $db->selectOneByCause($GLOBALS['categoryShortColArray'], CATEGORIES, array(CATEGORY_ID => $_REQUEST[PARAM_CID]));
        $template->assign( TABLETITLE, $cate[CATEGORY_TITLE]);
        
        // assign total count and total page
        $count = $db->selectCountRecord( SERVICES, array(SERVICE_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * view form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_SERVICES);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get id
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            $db = new DBAccess();
            $item = $db->selectOneByCause( $GLOBALS['serviceFullColArray'], SERVICES, array(SERVICE_ID => $_REQUEST[PARAM_ID]));
            $title = $GLOBALS['formTitleArray'][SERVICES][EDIT][$this->lan] . $item[SERVICE_TITLE];
        } else {
            $item = array();
            $item[SERVICE_CATEGORYID] = $_REQUEST[PARAM_CID];
            $title = $GLOBALS['formTitleArray'][SERVICES][ADD][$this->lan];
            $item[TOUR_HOT] = 0;
            $item[TOUR_FESH] = 0;
            $item[TOUR_PRICE] = 0;
            $item[TOUR_SHOWPRICE] = 0;
            $item[TOUR_CURRENCY] = "VND";
            $item[TOUR_SHOWCURRENCY] = "VND";
        }
        
        $item[CATEGORY_TYPE] = createSelectTag(CATEGORY_TYPE, "full-width", CATEGORY_ID, CATEGORY_TITLE, CATEGORIES, array(CATEGORY_ID => $item[SERVICE_CATEGORYID]), $item[SERVICE_CATEGORYID]);
        
        $item[TOUR_PRICE] = addComma( $item[TOUR_PRICE]);
        $item[TOUR_SHOWPRICE] = addComma( $item[TOUR_SHOWPRICE]);
        $item[TOUR_HOT] = createSelectTagFormData( TOUR_HOT, "full-width", $this->hotArray, $item[TOUR_HOT]);
        $item[TOUR_FESH] = createSelectTagFormData( TOUR_FESH, "full-width", $this->feshArray, $item[TOUR_FESH]);
        $item[TOUR_CURRENCY] = createSelectTagFormData( TOUR_CURRENCY, "full-width", $GLOBALS['currency'], $item[TOUR_CURRENCY]);
        $item[TOUR_SHOWCURRENCY] = createSelectTagFormData( TOUR_SHOWCURRENCY, "full-width", $GLOBALS['currency'], $item[TOUR_SHOWCURRENCY]);
        
        
        // assign gird title
        $template->assign( FORMTITLE, $title);
        
        // assign form item
        $template->assign( ITEM, $item);
        $template->parse( PAGECONTENT . "." . ITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * submit form function
     * @return multitype:string
     */
    public function submitForm() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        $item[SERVICE_ID] = $_REQUEST[SERVICE_ID];
        
        $item[SERVICE_TITLE] = $_REQUEST[SERVICE_TITLE];
        $item[SERVICE_IMAGELINK] = $_REQUEST[SERVICE_IMAGELINK];
        
        $item[SERVICE_CATEGORYID] = $_REQUEST[SERVICE_CATEGORYID];
        
        $item[SERVICE_PRICE] = $_REQUEST[SERVICE_PRICE];
        $item[SERVICE_CURRENCY] = $_REQUEST[SERVICE_CURRENCY];
        $item[SERVICE_SHOWPRICE] = $_REQUEST[SERVICE_SHOWPRICE];
        $item[SERVICE_SHOWCURRENCY] = $_REQUEST[SERVICE_SHOWCURRENCY];
        $item[SERVICE_PROMOTION] = $_REQUEST[SERVICE_PROMOTION];
        
        $item[SERVICE_DESCRIPTION] = $_REQUEST[SERVICE_DESCRIPTION];
        $item[SERVICE_CONTENT] = $_REQUEST[SERVICE_CONTENT];
        
        $item[SERVICE_TITLETAG] = $_REQUEST[SERVICE_TITLETAG];
        $item[SERVICE_URL] = encodeURL( $_REQUEST[SERVICE_URL]);
        $item[SERVICE_METAKEYWORD] = $_REQUEST[SERVICE_METAKEYWORD];
        $item[SERVICE_METADESCRIPTION] = $_REQUEST[SERVICE_METADESCRIPTION];
        
        $item[SERVICE_UPDATETIME] = getCurrentDateTime();
        $item[SERVICE_LANGUAGE] = $this->lan;
        $item[SERVICE_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[SERVICE_TITLETAG])){
            $item[SERVICE_TITLETAG] = $item[SERVICE_TITLE];
        }
        if (empty($item[SERVICE_URL])){
            $item[SERVICE_URL] = encodeURL( $_REQUEST[SERVICE_TITLE]);
        }
        if (empty($item[SERVICE_METAKEYWORD])){
            $item[SERVICE_METAKEYWORD] = $item[SERVICE_TITLE] . "," .encodeString($item[SERVICE_TITLE]);
        }
        if (empty($item[SERVICE_METADESCRIPTION])){
            $item[SERVICE_METADESCRIPTION] = $item[SERVICE_DESCRIPTION];
        }
        
        // check url
        if (checkURLExist( SERVICES, $item[SERVICE_URL], $item[SERVICE_ID])) {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = getMessage( MESSAGE_URL_EXISTED);
            
            // return repnose array;
            return $response_array;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[SERVICE_ID] == 0) {
            if ($db->insertData( $item, SERVICES)) {
                $new = $db->selectOneByCause( array(SERVICE_ID, SERVICE_URL), SERVICES, array(SERVICE_URL => $item[SERVICE_URL]));
                updateDataType(SERVICES, $new[SERVICE_ID], $new[SERVICE_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_SERVICES . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&cid=" . $item[SERVICE_CATEGORYID];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, SERVICES)) {
                updateDataType(SERVICES, $item[SERVICE_ID], $item[SERVICE_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_SERVICES . "&act=" . PVALUE_ACTION_VIEW_TABLE . "&cid=" . $item[SERVICE_CATEGORYID];
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            
            $db = new DBAccess();
            
            if ($db->deleteData( SERVICES, $_REQUEST[PARAM_ID])) {
                deleteDataType(SERVICES, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDeleteMany() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
            
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( SERVICES, $id)) {
                        deleteDataType(SERVICES, $id);
                    } else {
                        $delete = false;
                    }
                }
                
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>